/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.DefaultListModel;
import offset.nodes.client.editor.model.messages.GetTemplatesForNodeTypePath.TemplateInformation;
import offset.nodes.client.view.SimpleDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class ExistingTemplates extends javax.swing.JPanel {

    /** Creates new form ExistingTemplates */
    public ExistingTemplates(List<TemplateInformation> templates, String uuidSelected) {
        initComponents();
        init(templates, uuidSelected);
    }

    SimpleDialog container = null;

     public void setContainer(SimpleDialog container) {
        this.container = container;
    }

    class TemplatesListAdapter extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                container.doClose(SimpleDialog.OK);
            }
        }
    }
    
    class TemplateInformationWrapper {
        TemplateInformation template;
        
        public TemplateInformationWrapper(TemplateInformation template) {
            this.template = template;
        }
        
        public String toString() {
            return template.getPath();
        }

        public TemplateInformation getTemplate() {
            return template;
        }
    }

    public void init(List<TemplateInformation> templates, String uuidSelected) {
        templatesList.addMouseListener(new TemplatesListAdapter());
        
        TemplateInformation selected = null;

        for (TemplateInformation template : templates) {
            if (uuidSelected != null && template.getUuid().equals(uuidSelected))
                selected = template;
            ((DefaultListModel) templatesList.getModel()).addElement(new TemplateInformationWrapper(template));
        }

        if (selected != null)
            templatesList.setSelectedValue(selected, true);
        else
            templatesList.setSelectedIndex(0);
    }

    /**
     * Return the selected template
     * 
     * @return
     */
    public TemplateInformation getResult() {
        if (templatesList.getSelectedValue() == null)
            return null;

        return ((TemplateInformationWrapper) templatesList.getSelectedValue()).getTemplate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        templatesList = new javax.swing.JList();
        templatesLabel = new javax.swing.JLabel();

        templatesList.setModel(new DefaultListModel());
        jScrollPane1.setViewportView(templatesList);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        templatesLabel.setText(bundle.getString("existingTemplates.templates")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                    .addComponent(templatesLabel))
                .addGap(27, 27, 27))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(templatesLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1)
                .addGap(36, 36, 36))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel templatesLabel;
    private javax.swing.JList templatesList;
    // End of variables declaration//GEN-END:variables

}
